/**
 * Created by lx on 2017/9/15.
 */
angular.module('module.charge')
    .controller('ChargeTurnoverController', ChargeTurnoverController);
ChargeTurnoverController.$inject = ['$state', '$scope', '$localStorage', '$filter', 'ChargeService'];
function ChargeTurnoverController($state, $scope, $localStorage, $filter, ChargeService) {

    var vm = this;
    var shopid = $localStorage[environmental + "shopid"];
    vm.getTime = getTime;
    vm.dirtype = '1'; // 初始化页面显示选取时间
    vm.arrTime = [];
    vm.monthArr = []; // 初始化本月清算情况流水
    var flag = 0; // 初始化记录用户选取时间，默认本月
    getSettle(1, flag); // 初始化获取本月记录
    vm.changeTime = changeTime;
    /////////////////////////////////////////////////

    function changeTime() {
        var myvar = parseInt(vm.dirtype);
        if (myvar > flag) {
            getSettle(myvar, flag);
        } else if (myvar <= flag) {
            vm.dateFilter = function (e) {
                return e.index < myvar;
            }
        }
    }

    function getSettle(value, i) {
        if (i >= value) {
            //console.log(vm.arrTime,value,flag);
            getTime(vm.arrTime, value - flag, 0);
            flag = value;
            return
        }
        var settleDate = $filter('date')(((new Date).setDate(15) - i * 31 * 60 * 60 * 24 * 1000), 'yyyyMM');
        vm.arrTime.push(settleDate);
        getSettle(value, i + 1)
    }

    function getTime(arr, value, i) { // 根据传入的日期从接口查询数据
        if (i >= value) {
            return
        }
        var num = arr.length - 1 - i;
        // console.log("这里是准备查询的位置",num);
        var item = arr[num];
        // console.log(item);
        ChargeService.getMonthly(item, shopid).$promise.then(function (monthly) {
            monthly.index = num;
            vm.monthArr.push(monthly);
        }, function (error) {
        });
        getTime(arr, value, i + 1);
    }

}